<template>
  <div class="dashboard-page">
    <aside class="sidebar">
      <div class="profile-card">
        <img class="avatar" :src="adminAvatar" alt="Sarah Smith" />
        <div class="profile-info">
          <span class="name">Sarah Smith</span>
          <span class="role">Admin</span>
        </div>
      </div>

      <nav class="sidebar-menu">
        <template v-for="item in menu" :key="item.label">
          <RouterLink
            v-if="item.path"
            :to="item.path"
            :class="['menu-item', item.active && 'active']"
          >
            <i :class="['menu-icon', item.icon]"></i>
            <span>{{ item.label }}</span>
          </RouterLink>
          <button v-else :class="['menu-item', item.active && 'active']">
            <i :class="['menu-icon', item.icon]"></i>
            <span>{{ item.label }}</span>
          </button>
        </template>
      </nav>
    </aside>

    <main class="main-content">
      <header class="main-header">
        <div class="brand">
          <img :src="hospitalLogo" class="brand-logo" alt="Hospital Logo" />
          <div>
            <h1 class="page-title">Profile</h1>
            <p>Patient Information</p>
          </div>
        </div>

        <div class="header-actions">
          <button class="icon-btn"><i class="ri-notification-3-line"></i></button>
          <button class="icon-btn"><i class="ri-settings-3-line"></i></button>
        </div>
      </header>

      <section class="profile-card-detail">
        <header class="card-top">
          <h2>Patient Information</h2>
          <div class="card-actions">
            <RouterLink class="badge badge-danger" to="/patient-profile">Patient List</RouterLink>
            <RouterLink class="badge badge-success" to="/patient-form">Add Patient</RouterLink>
          </div>
        </header>

        <div class="profile-summary">
          <img class="patient-photo" :src="patientPhoto" alt="Patient" />
          <div class="summary-text">
            <h3>About Patient</h3>
            <p>{{ aboutPatientText }}</p>
            <div class="summary-grid">
              <div>
                <span class="label">Email</span>
                <span class="value">{{ activePatient.email || '—' }}</span>
              </div>
              <div>
                <span class="label">Phone Number</span>
                <span class="value">{{ activePatient.mobile || '—' }}</span>
              </div>
              <div>
                <span class="label">Age</span>
                <span class="value">{{ activePatient.age || '—' }}</span>
              </div>
              <div>
                <span class="label">Gender</span>
                <span class="value">{{ activePatient.gender || '—' }}</span>
              </div>
              <div>
                <span class="label">Blood Group</span>
                <span class="value">{{ activePatient.bloodGroup || '—' }}</span>
              </div>
              <div>
                <span class="label">Date of Birth</span>
                <span class="value">{{ activePatient.dob || '—' }}</span>
              </div>
              <div class="full">
                <span class="label">Address</span>
                <span class="value">{{ activePatient.address || '—' }}</span>
              </div>
            </div>
          </div>
        </div>

        <div class="profile-bottom">
          <div class="table-card mini">
            <header class="table-header">
              <h3>Past Visit History</h3>
              <button class="ghost-btn" type="button" @click="addVisit">+ Add Visit</button>
            </header>
            <table>
              <thead>
                <tr>
                  <th>Date</th>
                  <th>Doctor</th>
                  <th>Treatment</th>
                  <th>Charges</th>
                  <th>Actions</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="item in visits" :key="item.id">
                  <td>{{ item.date }}</td>
                  <td class="link-text">{{ item.doctor }}</td>
                  <td>
                    <span class="badge badge-warning">{{ item.treatment }}</span>
                  </td>
                  <td>{{ item.charge }}</td>
                  <td class="row-actions">
                    <button class="icon-btn" type="button" title="View" @click="viewVisit(item)"><i class="ri-eye-line"></i></button>
                    <button class="icon-btn" type="button" title="Delete" @click="removeVisit(item.id)"><i class="ri-delete-bin-line"></i></button>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>

          <div class="general-report">
            <header class="table-header">
              <h3>General Report</h3>
            </header>
            <ul>
              <li v-for="metric in metrics" :key="metric.title">
                <div class="metric-head">
                  <span>{{ metric.title }}</span>
                  <span>{{ metric.value }}</span>
                </div>
                <div class="metric-bar">
                  <div class="metric-fill" :style="{ width: metric.value + '%', background: metric.color }"></div>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </section>
    </main>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import { usePatientStore } from '../stores/patientStore'

const route = useRoute()
const patientStore = usePatientStore()

const adminAvatar = 'https://i.pravatar.cc/100?img=48'
const defaultPatient = {
  firstName: 'John',
  lastName: 'Smith',
  email: 'johnsmith@gmail.com',
  mobile: '+91 8584952524',
  age: '32',
  gender: 'male',
  maritalStatus: 'married',
  bloodGroup: 'O+',
  dob: '1990-04-15',
  address: '345, Saraj Appt, Mota Varacha, Surat Gujarat, India.',
  suggest: 'Patient recovering well. Requires monthly follow-up and consistent medication adherence.',
}

const activePatient = computed(() => patientStore.currentPatient ?? defaultPatient)

const patientPhoto = computed(
  () =>
    `https://api.dicebear.com/8.x/avataaars/svg?seed=${encodeURIComponent(
      activePatient.value.email || activePatient.value.firstName || 'patient'
    )}&backgroundColor=b6e3f4,c0aede,d1d4f9`
)

const aboutPatientText = computed(() => activePatient.value.suggest || defaultPatient.suggest)

const hospitalLogo =
  "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='64' height='64' viewBox='0 0 64 64'><defs><linearGradient id='a' x1='0%' y1='0%' x2='100%' y2='100%'><stop offset='0%' stop-color='%23ff86d5'/><stop offset='100%' stop-color='%23907cff'/></linearGradient></defs><rect x='4' y='4' width='56' height='56' rx='18' fill='%23ffffff' stroke='url(%23a)' stroke-width='4'/><path d='M32 14c9.9 0 18 8.1 18 18s-8.1 18-18 18S14 41.9 14 32 22.1 14 32 14zm0 6a12 12 0 100 24 12 12 0 000-24zm1.5 5v5.5H39v3H33.5V39h-3v-5.5H25v-3h5.5V25h3z' fill='url(%23a)'/></svg>"

const sidebarMenu = [
  { label: 'Menu', icon: 'ri-compass-3-line', path: '/' },
  { label: 'Dashboard', icon: 'ri-dashboard-2-line', path: '/dashboard' },
  { label: 'Patient Form', icon: 'ri-calendar-check-line', path: '/patient-form' },
  { label: 'Patient Profile', icon: 'ri-user-3-line', path: '/patient-profile' },
  { label: 'Payment', icon: 'ri-bill-line', path: '/payment-table' },
  { label: 'Reports', icon: 'ri-bar-chart-2-line', path: null },
  { label: 'Pharmacy', icon: 'ri-capsule-line', path: null },
  { label: 'Settings', icon: 'ri-equalizer-line', path: null },
  { label: 'Logout', icon: 'ri-shut-down-line', path: null },
]

const menu = computed(() =>
  sidebarMenu.map((item) => ({
    ...item,
    active: item.path === route.path,
  }))
)

const visits = ref([
  { id: 1, date: '12 Jan 2022', doctor: 'Dr Jacob Ryan', treatment: 'Check up', charge: '$145' },
  { id: 2, date: '12 Jan 2022', doctor: 'Dr Jacob Ryan', treatment: 'X-Ray', charge: '$52' },
  { id: 3, date: '12 Jan 2022', doctor: 'Dr Jacob Ryan', treatment: 'Blood Test', charge: '$52' },
])

const visitSeed = ref(visits.value.length + 1)

function addVisit() {
  const id = visitSeed.value
  visits.value.push({
    id: Date.now(),
    date: `${10 + id} Jan 2022`,
    doctor: (['Dr Jacob Ryan', 'Dr Rubina Delayer', 'Dr Smith Rayan'][id % 3]) ?? 'Dr Jacob Ryan',
    treatment: (['MRI', 'Vaccination', 'Physio'][id % 3]) ?? 'Check up',
    charge: '$' + (120 + id).toString(),
  })
  visitSeed.value += 1
}

function removeVisit(id: number) {
  visits.value = visits.value.filter((item) => item.id !== id)
}

function viewVisit(visit: (typeof visits.value)[number]) {
  window.alert(`就诊详情\n日期：${visit.date}\n医生：${visit.doctor}\n项目：${visit.treatment}\n费用：${visit.charge}`)
}

const metrics = [
  { title: 'Heart Beat', value: 93, color: '#f27aad' },
  { title: 'Blood Pressure', value: 89, color: '#947bff' },
  { title: 'Sugar', value: 60, color: '#6fd5ff' },
  { title: 'Haemoglobin', value: 80, color: '#78d7a4' },
]
</script>

<style scoped>
@import './shared-dashboard.css';

.profile-card-detail {
  background: var(--card-bg);
  border-radius: 32px;
  padding: 36px;
  box-shadow: var(--soft-shadow);
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-top h2 {
  margin: 0;
  font-size: 24px;
}

.card-actions {
  display: flex;
  gap: 16px;
}

.profile-summary {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 28px;
  align-items: start;
}

.patient-photo {
  width: 220px;
  height: 260px;
  border-radius: 26px;
  object-fit: cover;
  background: rgba(148, 123, 255, 0.16);
}

.summary-text h3 {
  margin: 0 0 12px;
}

.summary-text p {
  margin: 0 0 18px;
  color: var(--text-muted);
  line-height: 1.6;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px 28px;
}

.summary-grid .full {
  grid-column: span 2;
}

.label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
}

.value {
  font-weight: 600;
  color: var(--text-body);
}

.profile-bottom {
  display: grid;
  grid-template-columns: 1.7fr 1fr;
  gap: 28px;
}

.table-card {
  background: rgba(248, 249, 255, 0.8);
  border-radius: 26px;
  padding: 24px;
  box-shadow: var(--soft-shadow);
}

.table-card table {
  width: 100%;
  border-collapse: collapse;
}

.table-card th {
  text-align: left;
  font-size: 13px;
  color: var(--text-muted);
  padding-bottom: 12px;
}

.table-card td {
  padding: 12px 0;
  font-size: 14px;
  border-top: 1px solid rgba(213, 217, 232, 0.35);
}

.table-card tbody tr:first-child td {
  border-top: none;
}

.row-actions {
  display: flex;
  gap: 8px;
}

.general-report {
  background: rgba(248, 249, 255, 0.8);
  border-radius: 26px;
  padding: 24px 26px;
  box-shadow: var(--soft-shadow);
}

.general-report ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 18px;
}

.metric-head {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  font-weight: 600;
}

.metric-bar {
  margin-top: 10px;
  height: 10px;
  background: rgba(148, 123, 255, 0.18);
  border-radius: 999px;
  overflow: hidden;
}

.metric-fill {
  height: 100%;
  border-radius: 999px;
}

.link-text {
  color: #3681f7;
  font-weight: 600;
}
</style>

